
package com.hlkj.pay.service.finance.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.dto.finance.MerchantFinanceChargePageQueryDto;
import com.hlkj.pay.infrastructure.mapper.finance.MerchantFinanceChargeMapper;
import com.hlkj.pay.infrastructure.model.finance.MerchantFinanceChargeDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.finance.IMerchantFinanceChargeService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/11/14 16:25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MerchantFinanceChargeServiceImpl extends CommonSnFilterService implements IMerchantFinanceChargeService {

    private final MerchantFinanceChargeMapper merchantFinanceChargeMapper;

    @Override
    public PageResult<MerchantFinanceChargeDO> queryPage(MerchantFinanceChargePageQueryDto merchantFinanceChargePageQueryDto) {
        return merchantFinanceChargeMapper.selectPage(merchantFinanceChargePageQueryDto, buildWrapper(merchantFinanceChargePageQueryDto));
    }

    @Override
    public void addFinanceCharge(MerchantFinanceChargeDO merchantFinanceChargeDO) {
        merchantFinanceChargeMapper.insert(merchantFinanceChargeDO);
    }

    @Override
    public MerchantFinanceChargeDO queryCharge(String mchOrderNo) {
        return merchantFinanceChargeMapper.selectOne(MerchantFinanceChargeDO::getMchOrderNo, mchOrderNo);
    }

    @Override
    public void updateCharge(Long id, Integer state) {
        MerchantFinanceChargeDO merchantFinanceChargeDO=new MerchantFinanceChargeDO();
        merchantFinanceChargeDO.setId(id);
        merchantFinanceChargeDO.setState(state);
        merchantFinanceChargeMapper.updateById(merchantFinanceChargeDO);
    }

    private LambdaQueryWrapperX<MerchantFinanceChargeDO> buildWrapper(MerchantFinanceChargePageQueryDto financeChargePageQueryDto) {
        filterSn(financeChargePageQueryDto);
        LambdaQueryWrapperX<MerchantFinanceChargeDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.likeIfPresent(MerchantFinanceChargeDO::getMchOrderNo, financeChargePageQueryDto.getMchOrderNo());
        queryWrapper.likeIfPresent(MerchantFinanceChargeDO::getPayOrderNo, financeChargePageQueryDto.getPayOrderNo());
        queryWrapper.eqIfPresent(MerchantFinanceChargeDO::getPayWayCode, financeChargePageQueryDto.getPayWayCode());
        queryWrapper.eqIfPresent(MerchantFinanceChargeDO::getChargeType, financeChargePageQueryDto.getChargeType());
        queryWrapper.eqIfPresent(MerchantFinanceChargeDO::getState, financeChargePageQueryDto.getState());
        queryWrapper.eqIfPresent(MerchantFinanceChargeDO::getSn, financeChargePageQueryDto.getSn());
        queryWrapper.inIfPresent(MerchantFinanceChargeDO::getSn, financeChargePageQueryDto.getSnList());
        queryWrapper.eqIfPresent(MerchantFinanceChargeDO::getSysType, financeChargePageQueryDto.getSysType());

        queryWrapper.geIfPresent(MerchantFinanceChargeDO::getAmount, financeChargePageQueryDto.getMinAmount());
        queryWrapper.leIfPresent(MerchantFinanceChargeDO::getAmount, financeChargePageQueryDto.getMaxAmount());
        queryWrapper.geIfPresent(MerchantFinanceChargeDO::getCreateTime, financeChargePageQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(MerchantFinanceChargeDO::getCreateTime, financeChargePageQueryDto.getEndQueryTime());
        return queryWrapper;
    }
}
